/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    stm32f7xx_it.c
  * @brief   Interrupt Service Routines.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f7xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "ScnsInclude.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */

/* USER CODE END TD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */
static void TIM6_CALLBACK(void);
static void TIM7_CALLBACK(void);
extern void tpadInterrupt(void);
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/* External variables --------------------------------------------------------*/
extern DMA_HandleTypeDef hdma_dcmi;
extern DCMI_HandleTypeDef hdcmi;
extern DMA_HandleTypeDef hdma_sai1_a;
extern DMA_HandleTypeDef hdma_sai1_b;
extern SAI_HandleTypeDef hsai_BlockA1;
extern SAI_HandleTypeDef hsai_BlockB1;
/* USER CODE BEGIN EV */

/* USER CODE END EV */

/******************************************************************************/
/*           Cortex-M7 Processor Interruption and Exception Handlers          */
/******************************************************************************/
/**
  * @brief This function handles Non maskable interrupt.
  */
void NMI_Handler(void)
{
  /* USER CODE BEGIN NonMaskableInt_IRQn 0 */
    scnsYingErrorHandler(0,0,__FILE__,__LINE__,__FUNCTION__);
  /* USER CODE END NonMaskableInt_IRQn 0 */
  /* USER CODE BEGIN NonMaskableInt_IRQn 1 */

  /* USER CODE END NonMaskableInt_IRQn 1 */
}

/**
  * @brief This function handles Hard fault interrupt.
  */
void HardFault_Handler(void)
{
  /* USER CODE BEGIN HardFault_IRQn 0 */
    scnsYingErrorHandler(0,0,__FILE__,__LINE__,__FUNCTION__);
  /* USER CODE END HardFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_HardFault_IRQn 0 */
    /* USER CODE END W1_HardFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Memory management fault.
  */
void MemManage_Handler(void)
{
  /* USER CODE BEGIN MemoryManagement_IRQn 0 */
    scnsYingErrorHandler(0,0,__FILE__,__LINE__,__FUNCTION__);
  /* USER CODE END MemoryManagement_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
    /* USER CODE END W1_MemoryManagement_IRQn 0 */
  }
}

/**
  * @brief This function handles Pre-fetch fault, memory access fault.
  */
void BusFault_Handler(void)
{
  /* USER CODE BEGIN BusFault_IRQn 0 */
    scnsYingErrorHandler(0,0,__FILE__,__LINE__,__FUNCTION__);
  /* USER CODE END BusFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_BusFault_IRQn 0 */
    /* USER CODE END W1_BusFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Undefined instruction or illegal state.
  */
void UsageFault_Handler(void)
{
  /* USER CODE BEGIN UsageFault_IRQn 0 */
    scnsYingErrorHandler(0,0,__FILE__,__LINE__,__FUNCTION__);
  /* USER CODE END UsageFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_UsageFault_IRQn 0 */
    /* USER CODE END W1_UsageFault_IRQn 0 */
  }
}

/**
  * @brief This function handles System service call via SWI instruction.
  */
void SVC_Handler(void)
{
  /* USER CODE BEGIN SVCall_IRQn 0 */
    scnsYingErrorHandler(0,0,__FILE__,__LINE__,__FUNCTION__);
  /* USER CODE END SVCall_IRQn 0 */
  /* USER CODE BEGIN SVCall_IRQn 1 */

  /* USER CODE END SVCall_IRQn 1 */
}

/**
  * @brief This function handles Debug monitor.
  */
void DebugMon_Handler(void)
{
  /* USER CODE BEGIN DebugMonitor_IRQn 0 */
  /* USER CODE END DebugMonitor_IRQn 0 */
  /* USER CODE BEGIN DebugMonitor_IRQn 1 */

  /* USER CODE END DebugMonitor_IRQn 1 */
}

/**
  * @brief This function handles Pendable request for system service.
  */
void PendSV_Handler(void)
{
  /* USER CODE BEGIN PendSV_IRQn 0 */

  /* USER CODE END PendSV_IRQn 0 */
  /* USER CODE BEGIN PendSV_IRQn 1 */

  /* USER CODE END PendSV_IRQn 1 */
}

/**
  * @brief This function handles System tick timer.
  */
void SysTick_Handler(void)
{
  /* USER CODE BEGIN SysTick_IRQn 0 */

  /* USER CODE END SysTick_IRQn 0 */
  HAL_IncTick();
  /* USER CODE BEGIN SysTick_IRQn 1 */
    scnsTimerSysTickHandler();

  /* USER CODE END SysTick_IRQn 1 */
}

/******************************************************************************/
/* STM32F7xx Peripheral Interrupt Handlers                                    */
/* Add here the Interrupt Handlers for the used peripherals.                  */
/* For the available peripheral interrupt handler names,                      */
/* please refer to the startup file (startup_stm32f7xx.s).                    */
/******************************************************************************/

/**
  * @brief This function handles TIM2 global interrupt.
  */
void TIM2_IRQHandler(void)
{
  /* USER CODE BEGIN TIM2_IRQn 0 */
    tpadInterrupt();
  /* USER CODE END TIM2_IRQn 0 */
  /* USER CODE BEGIN TIM2_IRQn 1 */

  /* USER CODE END TIM2_IRQn 1 */
}

/**
  * @brief This function handles USART1 global interrupt.
  */
void USART1_IRQHandler(void)
{
  /* USER CODE BEGIN USART1_IRQn 0 */
    if(LL_USART_IsActiveFlag_IDLE(USART1))
    {
        LL_USART_ClearFlag_IDLE(USART1);
        scnsSmctpReceive(NULL,0,2);
    }
  /* USER CODE END USART1_IRQn 0 */
  /* USER CODE BEGIN USART1_IRQn 1 */
  /* USER CODE END USART1_IRQn 1 */
}

/**
  * @brief This function handles TIM6 global interrupt, DAC1 and DAC2 underrun error interrupts.
  */
void TIM6_DAC_IRQHandler(void)
{
  /* USER CODE BEGIN TIM6_DAC_IRQn 0 */
    if(LL_TIM_IsActiveFlag_UPDATE(TIM6)==SET)
    {
        LL_TIM_ClearFlag_UPDATE(TIM6);
        TIM6_CALLBACK();
    }
  /* USER CODE END TIM6_DAC_IRQn 0 */

  /* USER CODE BEGIN TIM6_DAC_IRQn 1 */

  /* USER CODE END TIM6_DAC_IRQn 1 */
}

/**
  * @brief This function handles TIM7 global interrupt.
  */
void TIM7_IRQHandler(void)
{
  /* USER CODE BEGIN TIM7_IRQn 0 */
    if(LL_TIM_IsActiveFlag_UPDATE(TIM7)==SET)
    {
        LL_TIM_ClearFlag_UPDATE(TIM7);
        TIM7_CALLBACK();
    }
  /* USER CODE END TIM7_IRQn 0 */
  /* USER CODE BEGIN TIM7_IRQn 1 */

  /* USER CODE END TIM7_IRQn 1 */
}

/**
  * @brief This function handles DMA2 stream1 global interrupt.
  */
void DMA2_Stream1_IRQHandler(void)
{
  /* USER CODE BEGIN DMA2_Stream1_IRQn 0 */

  /* USER CODE END DMA2_Stream1_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_dcmi);
  /* USER CODE BEGIN DMA2_Stream1_IRQn 1 */

  /* USER CODE END DMA2_Stream1_IRQn 1 */
}

/**
  * @brief This function handles DMA2 stream2 global interrupt.
  */
void DMA2_Stream2_IRQHandler(void)
{
  /* USER CODE BEGIN DMA2_Stream2_IRQn 0 */
    if(LL_DMA_IsActiveFlag_TC2(DMA2))
    {
        LL_DMA_ClearFlag_TC2(DMA2);
        scnsSmctpReceive(NULL,0,0);
    }
    if(LL_DMA_IsActiveFlag_HT2(DMA2))
    {
        LL_DMA_ClearFlag_HT2(DMA2);
        scnsSmctpReceive(NULL,0,1);
    }
    if(LL_DMA_IsActiveFlag_TE2(DMA2))
    {
        LL_DMA_ClearFlag_TE2(DMA2);
        hxPrintfFL("LL_DMA_IsActiveFlag_TE2");
    }
  /* USER CODE END DMA2_Stream2_IRQn 0 */

  /* USER CODE BEGIN DMA2_Stream2_IRQn 1 */

  /* USER CODE END DMA2_Stream2_IRQn 1 */
}

/**
  * @brief This function handles DMA2 stream3 global interrupt.
  */
void DMA2_Stream3_IRQHandler(void)
{
  /* USER CODE BEGIN DMA2_Stream3_IRQn 0 */

  /* USER CODE END DMA2_Stream3_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_sai1_a);
  /* USER CODE BEGIN DMA2_Stream3_IRQn 1 */

  /* USER CODE END DMA2_Stream3_IRQn 1 */
}

/**
  * @brief This function handles DMA2 stream4 global interrupt.
  */
void DMA2_Stream4_IRQHandler(void)
{
  /* USER CODE BEGIN DMA2_Stream4_IRQn 0 */

  /* USER CODE END DMA2_Stream4_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_sai1_b);
  /* USER CODE BEGIN DMA2_Stream4_IRQn 1 */

  /* USER CODE END DMA2_Stream4_IRQn 1 */
}

/**
  * @brief This function handles DMA2 stream7 global interrupt.
  */
void DMA2_Stream7_IRQHandler(void)
{
  /* USER CODE BEGIN DMA2_Stream7_IRQn 0 */
    if(LL_DMA_IsActiveFlag_TC7(DMA2))
    {
        LL_DMA_ClearFlag_TC7(DMA2);
        LL_DMA_DisableStream(DMA2,LL_DMA_STREAM_7);
        scnsSmctpSendFinishCallback();
    }
    if(LL_DMA_IsActiveFlag_TE7(DMA2))
    {
        LL_DMA_ClearFlag_TE7(DMA2);
        LL_DMA_DisableStream(DMA2,LL_DMA_STREAM_7);
        hxPrintfFL("LL_DMA_IsActiveFlag_TE7");
        scnsSmctpSendFinishCallback();
    }
  /* USER CODE END DMA2_Stream7_IRQn 0 */

  /* USER CODE BEGIN DMA2_Stream7_IRQn 1 */

  /* USER CODE END DMA2_Stream7_IRQn 1 */
}

/**
  * @brief This function handles DCMI global interrupt.
  */
void DCMI_IRQHandler(void)
{
  /* USER CODE BEGIN DCMI_IRQn 0 */

  /* USER CODE END DCMI_IRQn 0 */
  HAL_DCMI_IRQHandler(&hdcmi);
  /* USER CODE BEGIN DCMI_IRQn 1 */

  /* USER CODE END DCMI_IRQn 1 */
}

/**
  * @brief This function handles SAI1 global interrupt.
  */
void SAI1_IRQHandler(void)
{
  /* USER CODE BEGIN SAI1_IRQn 0 */

  /* USER CODE END SAI1_IRQn 0 */
  HAL_SAI_IRQHandler(&hsai_BlockA1);
  HAL_SAI_IRQHandler(&hsai_BlockB1);
  /* USER CODE BEGIN SAI1_IRQn 1 */

  /* USER CODE END SAI1_IRQn 1 */
}

/* USER CODE BEGIN 1 */

static void TIM6_CALLBACK(void)
{
#if defined(SCNS_LED_ENABLE)&&SCNS_LED_ENABLE==1
    static const ScnsLedEnum group[]={
        SCNS_LED_0,
    };
    scnsLedGroupFlow(group,(sizeof(group))/(sizeof(group[0])),250000);
#endif
    if(scnsTimerGetInitedTick()!=0)
    {
#if defined(SCNS_FT5206_ENABLE)&&SCNS_FT5206_ENABLE==1
        scnsFt5206Interrupt(SCNS_FT5206_1);
#endif
#if defined(SCNS_TOUCH_SCREEN_ENABLE)&&SCNS_TOUCH_SCREEN_ENABLE==1
        scnsTouchScreenScan();
#endif
#if defined(SCNS_AP3216C_ENABLE)&&SCNS_AP3216C_ENABLE==1
        scnsAp3216cInterrupt(SCNS_AP3216C_1);
#endif
#if defined(SCNS_DS18B20_ENABLE)&&SCNS_DS18B20_ENABLE==1
        scnsDs18b20Interrupt(SCNS_DS18B20_1);
#endif
#if defined(SCNS_DHT11_ENABLE)&&SCNS_DHT11_ENABLE==1
        scnsDht11Interrupt(SCNS_DHT11_1);
#endif
    }
#if defined(SCNS_KEYBOARD_GPIO_ENABLE)&&SCNS_KEYBOARD_GPIO_ENABLE==1
    scnsKeyboardGpioInterrupt();
#endif
#if defined(SCNS_KEY_ENABLE)&&SCNS_KEY_ENABLE==1
    scnsKeyCallback();
#endif
}

static void TIM7_CALLBACK(void)
{
#if defined(SCNS_BEEP_ENABLE)&&SCNS_BEEP_ENABLE==1
    scnsBeepInterrupt();
#endif
    if(scnsTimerGetInitedTick()!=0)
    {
#if defined(SCNS_GYRO_ENABLE)&&SCNS_GYRO_ENABLE==1
        scnsGyroInterrupt(SCNS_GYRO_1);
#endif
    }
#if defined(SCNS_PCF8574_ENABLE)&&SCNS_PCF8574_ENABLE==1
    scnsPcf8574Sync(SCNS_PCF8574_1);
    scnsPcf8574Sync(SCNS_PCF8574_2);
    scnsPcf8574Sync(SCNS_PCF8574_3);
#endif
#if defined(SCNS_SMCTP_ENABLE)&&SCNS_SMCTP_ENABLE==1
    scnsSmctpFlushBuf();
#endif
}

void HAL_DCMI_FrameEventCallback(DCMI_HandleTypeDef*hdcmi)
{
#if defined(SCNS_OV7725_ENABLE)&&SCNS_OV7725_ENABLE==1
    scnsOv7725DmaFinishInterrupt(SCNS_OV7725_1);
#endif
#if defined(SCNS_OV2640_ENABLE)&&SCNS_OV2640_ENABLE==1
    scnsOv2640DmaFinishInterrupt(SCNS_OV2640_1);
#endif
#if defined(SCNS_OV5640_ENABLE)&&SCNS_OV5640_ENABLE==1
    scnsOv5640DmaFinishInterrupt(SCNS_OV5640_1);
#endif
}

void HAL_SAI_TxHalfCpltCallback(SAI_HandleTypeDef*hsai)
{
    if(hsai==&hsai_BlockA1)
    {
        scnsWm8978TxDmaFinishCallback(SCNS_WM8978_1,1);
    }
}

void HAL_SAI_TxCpltCallback(SAI_HandleTypeDef*hsai)
{
    if(hsai==&hsai_BlockA1)
    {
        scnsWm8978TxDmaFinishCallback(SCNS_WM8978_1,0);
    }
}

void HAL_SAI_RxHalfCpltCallback(SAI_HandleTypeDef*hsai)
{
    if(hsai==&hsai_BlockB1)
    {
        scnsWm8978RxDmaFinishCallback(SCNS_WM8978_1,1);
    }
}

void HAL_SAI_RxCpltCallback(SAI_HandleTypeDef*hsai)
{
    if(hsai==&hsai_BlockB1)
    {
        scnsWm8978RxDmaFinishCallback(SCNS_WM8978_1,0);
    }
}

/* USER CODE END 1 */
